package com.maoye.ossp.sys.site.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.site.entity.Site;
import com.maoye.ossp.sys.site.entity.SiteNav;
import com.maoye.ossp.sys.site.service.ISiteNavService;
import com.maoye.ossp.sys.site.service.ISiteService;
import com.maoye.ossp.sys.site.utils.BeanToMap;

/**
 * 站点管理Contorller
 * @author lijun
 *
 */
@Controller
@RequestMapping(value = "sys/site/")
public class SiteContorller extends BaseController {
	
	/**
	 * 是否可以提交 (1是 0否)
	 */
	private static final String IS_RESULT_SUCCESS = "1";
	private static final String IS_RESULT_FAIL = "0";

	@Resource
	private ISiteService siteService;

	@Resource
	private ISiteNavService siteNavService;
	
	@RequestMapping(value = "list")
	public String list(Site site,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<Site> list=siteService.getList(BeanToMap.beanToMap(site));
		model.addAttribute("list", list);
		model.addAttribute("site", site);
		return "/sys/site/list";
	}
	
	@RequestMapping(value = "{siteId}/view")
	public String view(@PathVariable Long siteId, Model model,SiteNav siteNav) {
		Site site = siteService.get(siteId);
		siteNav.setId(null);
		siteNav.setSiteId(siteId);
		List<SiteNav> list = siteNavService.getList(BeanToMap.beanToMap(siteNav));
		model.addAttribute("site", site);
		model.addAttribute("siteNav", siteNav);
		model.addAttribute("list", list);
		return "/sys/site/view";
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, Model model) {
		Site site = siteService.get(id);
		model.addAttribute("site", site);
		return "/sys/site/add";
	}
	
	@RequestMapping(value = "add")
	public String add(Site site,Model model) {
		site.setId(0L);
		model.addAttribute("site", site);
		return "/sys/site/add";
	}
	
	@RequestMapping(value = "save")
	public String save(Site site, Model model,
			RedirectAttributes redirectAttributes) {
		if (site.getId() == 0) {
			site.setId(idGenerator.getUId());
			siteService.create(site);
		} else {
			siteService.update(site);
		}
		addMessage(redirectAttributes, "保存数据成功");
		return "redirect:/sys/site/list";
	}
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		siteService.remove(id);
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/site/list";
	}
	
	/**
	 * 验证编码是否重复
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "validate")
	public void validate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String code = request.getParameter("code");
		String notId = request.getParameter("notId");
		
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		Site site = new Site();
		site.setCode(code);
		site.setNotId(notId);
		List<Site> list = siteService.getList(BeanToMap.beanToMap(site));
		StringBuffer result = new StringBuffer();
		if(list.size() > 0){
			result.append(IS_RESULT_FAIL);
		}else{
			result.append(IS_RESULT_SUCCESS);
		}
		out.write(result.toString());
		out.flush();
		out.close();
	}
}
